<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/goods/Goods' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.deptName" placeholder="输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label>
        <el-input size="small" v-model="formInline.deptNo" placeholder="输入商品代码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      stripe
      border
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="name" label="商品名称" width="240"></el-table-column>
      <el-table-column label="添加日期" width="240">
        <template slot-scope="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column prop="address" label="发往地址" width="400"></el-table-column>
      <el-table-column prop="people" label="负责人" width="240"></el-table-column>
      <el-table-column label="操作" width="240">
        <template slot-scope="scope">
          <el-button size="medium" type="info" @click="handleEdit()">编辑</el-button>
          <el-button size="medium" type="danger" @click="handleDel()">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 弹框 -->
    <el-dialog :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="商品名称" prop="deptName">
          <el-input
            size="small"
            v-model="editForm.deptName"
            auto-complete="off"
            placeholder="请输入商品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="deptNo">
          <el-input size="small" v-model="editForm.deptNo" auto-complete="off" placeholder="请输入负责人"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('editForm')"
        >保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { deptList, deptSave, deptDelete } from "../../api/userMG";
import Pagination from "../../components/Pagination.vue";
export default {
  data() {
    return {
      formInline: {
        deptName: "",
        deptNo: ""
      },
      tableData: [
        {
          date: "2020-05-03",
          name: "肥宅快乐水",
          address: "广州大学菊苑第7栋B21五楼520宿舍",
          people: "杨小星"
        },
        {
          date: "2020-05-03",
          name: "肥宅快乐水",
          address: "广州大学菊苑第7栋B21五楼520宿舍",
          people: "杨小星"
        },
        {
          date: "2020-05-03",
          name: "肥宅快乐水",
          address: "广州大学菊苑第7栋B21五楼520宿舍",
          people: "杨小星"
        },
        {
          date: "2020-05-03",
          name: "肥宅快乐水",
          address: "广州大学菊苑第7栋B21五楼520宿舍",
          people: "杨小星"
        },
        {
          date: "2020-05-03",
          name: "肥宅快乐水",
          address: "广州大学菊苑第7栋B21五楼520宿舍",
          people: "杨小星"
        },
        {
          date: "2020-05-03",
          name: "肥宅快乐水",
          address: "广州大学菊苑第7栋B21五楼520宿舍",
          people: "杨小星"
        },
        {
          date: "2020-05-03",
          name: "肥宅快乐水",
          address: "广州大学菊苑第7栋B21五楼520宿舍",
          people: "杨小星"
        },
        {
          date: "2020-05-03",
          name: "肥宅快乐水",
          address: "广州大学菊苑第7栋B21五楼520宿舍",
          people: "杨小星"
        }
      ],
      //避免重复点击按钮
      loading: false,
      multipleSelection: [],
      //控制弹框的关闭与打开
      editFormVisible: false,
      editForm: {
        deptId: "",
        deptName: "",
        deptNo: "",
        token: localStorage.getItem("logintoken")
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: "请输入商品名称", trigger: "blur" }
        ],
        deptNo: [{ required: true, message: "请输入负责人", trigger: "blur" }]
      },
      //分页数据
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      formInline: {
        page: 1,
        limit: 10,
        varLable: "",
        varName: "",
        token: localStorage.getItem("logintoken")
      }
    };
  },
  components:{
    Pagination
  },
  methods: {
    search() {},
    //打开对话框
    handleEdit() {
      this.editFormVisible = true;
    },
    //关闭对话框
    closeDialog() {
      this.editFormVisible = false;
    },
    //分页事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getdata(this.formInline);
    },
    //删除事件
    handleDel() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //提交编辑内容
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          deptSave(this.editForm)
            .then(res => {
              this.editFormVisible = false;
              this.loading = false;
              if (res.success) {
                this.getdata(this.formInline);
                this.$message({
                  type: "success",
                  message: "公司保存成功！"
                });
              } else {
                this.$message({
                  type: "info",
                  message: res.msg
                });
              }
            })
            .catch(err => {
              this.editFormVisible = false;
              this.loading = false;
              this.$message.error("公司保存失败，请稍后再试！");
            });
        } else {
          return false;
        }
      });
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>